import { service } from '@/api/api';
import { LocalNotifications } from '@capacitor/local-notifications';
// import { Network } from '@capacitor/network';
import { ref } from 'vue'; 
const  cachedAlertId = ref('');
const MAX_RETRIES = 3;
let retryCount = 0;
let num=0
export default async () => {
  // const status = await Network.getStatus();
  // if (!status.connected) return; // 无网络时跳过
  try {
    const response = await service.post('/alertApi/getAlerts', { length: '10' });
    const Alert = response.data.data.list[0];
    
    if (Alert.alertId !== cachedAlertId.value) {
      num++
      await LocalNotifications.schedule({
        notifications: [{
          title: 'new message',
          body: Alert,
          // id: Date.now(),
          id: num,
          schedule: { allowWhileIdle: true,at: new Date() },
          extra: { 
            alertId: Alert,
            deepLink: `baApp://alerts/${Alert}`
          },
        }]
      });
      // cachedAlertId.value = Alert.value;
    }
    cachedAlertId.value = Alert.alertId;
    return { success: true };
  } catch (error) {
    if (retryCount < MAX_RETRIES) {
      retryCount++;
      return { retry: true, delay: 60 };
    }
    return { error: error.message };
  }
};

